package gms.service.match;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import gms.dao.match.MatchApplyDao;
import gms.entities.match.MatchApply;
import gms.entities.match.MatchInfo;
import gms.util.PageBean;

/**
 * 赛事申请表服务实现类
 * @author Administrator
 *
 */
@Transactional
public class MatchApplyServiceImp implements MatchApplyService{
	MatchApplyDao matchApplyDao;  //赛事申请表DAO

	public void setMatchApplyDao(MatchApplyDao matchApplyDao) {
		this.matchApplyDao = matchApplyDao;
	}

	/**
	 * 保存赛事申请表服务
	 */
	@Override
	public boolean save(MatchApply matchApply) {		
		return matchApplyDao.save(matchApply);
	}

	/**
	 * 分页查找赛事申请表服务
	 */
	@Override
	public PageBean<MatchApply> findByPage(int currPage) {
		PageBean<MatchApply> pageBean = new PageBean<MatchApply>();
		//封装当前页数
		pageBean.setCurrPage(currPage);
		//每页显示的记录数
		int pageSize = 3;
		pageBean.setPageSize(pageSize);
		//封装总记录数
		int totalCount = matchApplyDao.findCount(); 
		pageBean.setTotalCount(totalCount);
		//封装总页数
		double tc = totalCount;
		Double num = Math.ceil(tc / pageSize); 
		pageBean.setTotalPage(num.intValue());
		//封装每页显示的数据
		int begin = (currPage - 1) * pageSize;
		List<MatchApply> list = matchApplyDao.findByPage(begin,pageSize);
		pageBean.setList(list);
		return pageBean;
	}

	/**
	 * 审核申请成功的赛事申请表
	 */
	@Override
	public List<MatchApply> examine() {
		String state = "申请成功";
		return matchApplyDao.findByState(state);
	}
	
	/**
	 * 审核该ID的赛事申请表
	 */
	@Override
	public MatchApply findByID(int matchApplyID) {
		//获得要审核ID的赛事申请表
		return matchApplyDao.findByID(matchApplyID);
	}

	/**
	 * 赛事申请表列表进行分页处理
	 */
	@Override
	public PageBean<MatchApply> listPage(int currPage,List<MatchApply> list) {
		PageBean<MatchApply> pageBean = new PageBean<MatchApply>();
		//封装当前页数
		pageBean.setCurrPage(currPage);
		//每页显示的记录数
		int pageSize = 3;
		pageBean.setPageSize(pageSize);
		//封装总记录数
		pageBean.setTotalCount(list.size());
		//封装总页数
		double tc = list.size();
		Double totalPage = Math.ceil(tc / pageSize); 
		pageBean.setTotalPage(totalPage.intValue());
		//封装每页显示的数据
		if(list.size() < 3){
			pageBean.setList(list);
		}else{
			if(currPage != totalPage){
				List<MatchApply> l = list.subList((currPage - 1) * pageSize, currPage * pageSize);
				pageBean.setList(l);
			}else{
				List<MatchApply> l = list.subList((currPage - 1) * pageSize, list.size());
				pageBean.setList(l);
			}
		}
		return pageBean;
	}

	/**
	 * 审核通过，更新审核状态为审核通过
	 */
	@Override
	public void exPassed(MatchApply matchApply) {
		matchApply.setAuditStatus("审核通过");
		matchApplyDao.update(matchApply);
	}

	/**
	 * 审核不通过，更新审核状态为审核不通过
	 */
	@Override
	public void exFailed(MatchApply matchApply) {
		matchApply.setAuditStatus("审核不通过");
		matchApplyDao.update(matchApply);
	}

	@Override
	public void toMatchInfo(MatchApply matchApply) {
		MatchInfo matchInfo = new MatchInfo();
		matchInfo.setPlaceID(matchApply.getPlaceID());
		matchInfo.setProposerName(matchApply.getProposerName());
		matchInfo.setProposerPhone(matchApply.getProposerPhone());
		matchInfo.setMatchName(matchApply.getMatchName());
		matchInfo.setMatchProject(matchApply.getMatchProject());
		matchInfo.setStartTime(matchApply.getStartTime());
		matchInfo.setEndTime(matchApply.getEndTime());
		matchInfo.setPersonInCharge(matchApply.getPersonInCharge());
		matchApplyDao.toMatchInfo(matchInfo);
		
	}

	/**
	 * 删除赛事申请表
	 */
	@Override
	public void delete(MatchApply matchApply) {
		matchApplyDao.delete(matchApply);
		
	}


	
}
